<template>
    <div class="change-password" style="border: skyblue 1px solid;border-radius:50px" >
        <div id="LeftTop" style="text-align:center;" >修改密码</div>
        <el-form ref="form" :model="form" :rules="rules" label-width="100px" class="change-password-form">
            <el-form-item label="旧密码" prop="oldPassword">
                <el-input type="password" v-model="form.oldPassword" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="新密码" prop="newPassword">
                <el-input type="password" v-model="form.newPassword" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="confirmPassword">
                <el-input type="password" v-model="form.confirmPassword" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <div class="MulButton">
                    <el-button type="primary" @click="submitForm" style="margin-left: -20px">提交</el-button>
                    <el-button @click="resetForm" style="margin-left: 50px">重置</el-button>
                </div>
            </el-form-item>
        </el-form>
    </div>
</template>

<script >
    export default {
        name: 'RevisePassWord',
        data() {
            /* password密码验证条件 */
            var checkPassword = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('请输入密码'));
                }else if (value === this.form.oldPassword){
                    return callback(new Error('新密码和原密码一致'));
                } else {
                    callback();
                }
            };
            /* confirmPassword确认密码验证条件 */
            var checkConfirmPassword = (rule, value, callback) => {
                if (!value) {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.form.newPassword) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                //修改密码的表单
                form: {
                    oldPassword: '',
                    newPassword: '',
                    confirmPassword: '',
                },
                rules: {
                    oldPassword: [
                        { required: true, message: '请输入旧密码', trigger: 'blur' },
                    ],
                    newPassword: [
                        { required: true, validator: checkPassword, trigger: 'blur' },
                        { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
                    ],
                    confirmPassword: [
                        { required: true,validator: checkConfirmPassword, trigger: 'blur' },
                        { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
                    ],
                },
            }
        },
        methods: {
            submitForm() {  // 提交表单
                this.$refs.form.validate(valid => {
                    if (valid) {
                        // console.log("===========表单验证成功=================")
                        this.$http.post("/system/api/user/revisePassWord",this.form).then(result=>{
                            if (result.data.data){
                                this.$message({
                                    showClose: true,
                                    message: result.data.msg,
                                    type: 'success'
                                });
                                this.$http.get("/system/api/user/quitSystem").then(result=>{
                                    //清空浏览器中的token
                                    sessionStorage.clear();
                                    this.$router.push("/")
                                })
                            }else {
                                this.$message({
                                    showClose: true,
                                    message: result.data.msg,
                                    type: 'error'
                                });
                            }
                        })
                        //修改完或修改失败后清空表单
                        this.resetForm();
                    } else {
                        return false
                    }
                })

            },
            resetForm() {  //清空表单
                this.$refs.form.resetFields()
            },
        },
    }
</script>

<style>
    .change-password {
        max-width: 400px;
        margin: 0 auto;
        padding: 40px 20px;
    }

    .change-password-form {
        margin-top: 40px;
        margin-right: 40px;
    }
    #LeftTop{
        text-align: center;
        color: skyblue;
        font-size: xx-large;
        font-family: 华文行楷;
    }
    .MulButton{
        text-align: center;
    }
</style>
